package com.wangx.tools;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;

public class Leet_Json_Table {
    private static Robot robot;
    public static void main(String[] args) throws IOException {

//        Scanner scan = new Scanner(System.in);
//        System.out.println("请输入json");
//        String s = scan.nextLine();

        // 获取剪切板内容
        String s = getSysClipboardText();
        // 将: [ ==>  :[
        s = s.replace(": ", ":");
        s = s.replace(", ", ",");

        // 1. 切分成一个个单词:  split
        String replace = s.replace("{", ",").replace("}", ",")
                .replace("[", ",").replace("]", ",")
                .replace("\"", ",").replace(":", ",")
                .replace(";", ",");
        String[] split1 = replace.split(",");
        String[] split = deleteArrayNull(split1);
//            for (int i = 0; i < split.length; i++) {
//                System.out.println(split[i]);
//            }

        // 2.求表的数量
        int biaocount = 0;
        int i1 = testCount(s, ":");
        biaocount = (i1 - 2) / 2;
        //System.out.println("表的数量 = " + biaocount);

        // 3. 求字段数量
        //   3.1 表[]的字段数量
        int[] ziduancount = new int[biaocount];
        String[] splitzd = s.split("rows");
        String[] splitzd2 = splitzd[0].split("\\[");
        for (int i = 0; i < biaocount; i++) {
            // 每个表的字段数
            ziduancount[i] = splitzd2[i + 1].split(",").length - 1;
            int i2 = i + 1;
            //System.out.println("表"+ i2+"的字段数量为:"+ ziduancount[i]);
        }

        // 4. 求表的行数
        //    表[]的行数
        int[] hangshu_count = new int[biaocount];
        String[] spliths = splitzd[1].split("\\[\\[");
        for (int i = 0; i < biaocount; i++) {
            // 每个表数据的行数
            hangshu_count[i] = spliths[i + 1].split("\\]").length - 2;
            int i2 = i + 1;
            //System.out.println("表"+ i2+"的行数为:"+ hangshu_count[i]);
        }

        // 1. 用于存表名
        String[] biao_name = new String[biaocount];
        // 2. 存列名
        int ziduan_max = 0;
        for (int i = 0; i < biaocount; i++) {
            if (ziduan_max < ziduancount[i]) {
                ziduan_max = ziduancount[i];
            }
        }
        //System.out.println(ziduan_max);
        String[][] ziduan_name = new String[biaocount][ziduan_max];

        // 3. 存表n数据
        // 行数最大值
        int hang_max = 0;
        for (int i = 0; i < biaocount; i++) {
            if (hang_max < hangshu_count[i]) {
                hang_max = hangshu_count[i];
            }
        }
        //System.out.println("hang_max = " + hang_max);
        String[][][] biao_hang = new String[biaocount][hang_max][ziduan_max];
        // 4. 全数据
        String[] quan_shuju = new String[split.length];

        // 1. 全数据装载
        for (int i = 0; i < split.length; i++) {
            quan_shuju[i] = split[i];
        }

        // 2. 表名装载
        int a = 1;
        for (int i = 0; i < biaocount; i++) {
            biao_name[i] = quan_shuju[a];
            a = a + ziduancount[i] + 1;
        }

        // 3. 列名数据装载
        int b = 2;
        for (int i = 0; i < biaocount; i++) {
            for (int i2 = 0; i2 < ziduancount[i]; i2++) {
                ziduan_name[i][i2] = quan_shuju[b];
                b++;
            }
            b++;
        }
        //System.out.println("b = " + ziduan_name[1][1]);

        // 4. 表n的数据装载
        // 字段总数c
        int c = 0;
        for (int i = 0; i < biaocount; i++) {
            c = c + ziduancount[i];
        }
        //System.out.println("c = " + c);
        // 12 开始       224
        int d = c + biaocount + 3;
        //System.out.println(d);
        for (int i = 0; i < biaocount; i++) {
            for (int i2 = 0; i2 < hangshu_count[i]; i2++) {
                for (int i3 = 0; i3 < ziduancount[i]; i3++) {
                    biao_hang[i][i2][i3] = quan_shuju[d];
                    d++;
                }
            }
            d++;
        }
        //System.out.println(biao_hang[1][1][1]);


        // ① 全语句
        String q = "";

        // 1. 拼接建表语句
        for (int i = 0; i < biaocount; i++) {
            String s1 = "DROP TABLE IF EXISTS " + biao_name[i] + ";\n" + "CREATE TABLE " + "IF NOT EXISTS ";
            s1 = s1.concat(biao_name[i] + "(" + "\n");
            for (int i2 = 0; i2 < ziduancount[i]; i2++) {
                if (isNumeric(biao_hang[i][0][i2])) {
                    s1 = s1.concat("\t" + ziduan_name[i][i2] + " INT");
                } else {
                    s1 = s1.concat("\t" + ziduan_name[i][i2] + " VARCHAR(50)");
                }
                if (i2 + 1 != ziduancount[i]) {
                    s1 = s1.concat("," + "\n");
                } else {
                    s1 = s1.concat("\n" + ");");
                }
            }
            //s1 = s1.concat("\n");
            q = q.concat(s1+"\n");
            //System.out.println(s1);
        }

        // 2. 拼接装载语句
        for (int i = 0; i < biaocount; i++) {
            String s2 = "insert into ";
            s2 = s2.concat(biao_name[i] + "(");
            for (int i2 = 0; i2 < ziduancount[i]; i2++) {
                s2 = s2.concat(ziduan_name[i][i2]);
                if (i2 + 1 != ziduancount[i]) {
                    s2 = s2.concat(",");
                } else {
                    s2 = s2.concat(") values");
                }
            }

            for (int i2 = 0; i2 < hangshu_count[i]; i2++) {
                s2 = s2.concat("(");
                for (int i3 = 0; i3 < ziduancount[i]; i3++) {
                    if (isNumeric(biao_hang[i][i2][i3])) {
                        s2 = s2.concat(biao_hang[i][i2][i3]);
                    } else if (biao_hang[i][i2][i3].equals("null")){
                        s2 = s2.concat(biao_hang[i][i2][i3]);
                    }else {
                        s2 = s2.concat("\"" + biao_hang[i][i2][i3] + "\"");
                    }
                    if (i3 + 1 != ziduancount[i]) {
                        s2 = s2.concat(",");
                    }
                }
                s2 = s2.concat(")");
                if (i2 + 1 != hangshu_count[i]) {
                    s2 = s2.concat(",");
                } else {
                    s2 = s2.concat(";");
                }
            }
            //System.out.println(s2);
            q = q.concat(s2+"\n");
        }

        // 将内容放进剪切板
        setSysClipboardText(q);
        System.out.println(q);

        try {
            robot = new Robot();//创建Robot对象
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 延时
        int a1 = 50;
        // 切换程序
        robot.keyPress(KeyEvent.VK_ALT);
        robot.delay(a1);//延迟50ms
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_ALT);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.delay(a1);//延迟50ms

        // 保证到最后一行
        robot.keyPress(KeyEvent.VK_END);
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_END);
        robot.delay(a1);//延迟50ms
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(a1);//延迟50ms

        // ctrl+v
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.delay(a1);//延迟50ms
        robot.keyPress(KeyEvent.VK_V);
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);
        robot.delay(a1);//延迟50ms

       // 求行数
        int b2 =0;
        for (int i = 0; i < biaocount; i++) {
            b2=b2+ziduancount[i];
        }
        int b1 = 4 * biaocount + b2;
        System.out.println("b1 = " + b1);
        //选择代码
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.delay(a1);//延迟50ms
        for (int i = 0; i < b1; i++) {
            robot.keyPress(KeyEvent.VK_UP);
            robot.keyRelease(KeyEvent.VK_UP);
        }
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.delay(a1);//延迟50ms

        //  执行DG
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.delay(a1);//延迟50ms
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(a1);//延迟50ms
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ENTER);

    }

    // 1. 求某字符串出现的数量
    public static int testCount(String s, String s2) {
        int count = 0;
        /*获取初始字符串长度*/
        int sl = s.length();
        /*String.replace(s1,s2)方法作用：将字符串中s1替换为s2
        将其中字符a替换为”“*/
        String str = s.replace(s2, "");
        /*替换后的字符串长度*/
        int strl = str.length();
        count = sl - strl;
        return count;
    }


    // 2. 去除String数组中的空值
    private static String[] deleteArrayNull(String string[]) {
        String strArr[] = string;

        // 定义一个list列表，并循环赋值
        ArrayList<String> strList = new ArrayList<String>();
        for (int i = 0; i < strArr.length; i++) {
            strList.add(strArr[i]);
        }

        // 删除list列表中所有的空值
        while (strList.remove(null)) ;
        while (strList.remove("")) ;

        // 把list列表转换给一个新定义的中间数组，并赋值给它
        String strArrLast[] = strList.toArray(new String[strList.size()]);

        return strArrLast;
    }

    // 3., 判断是否为数字
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            //System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 4. 从剪切板获得文字。
     */
    public static String getSysClipboardText() {
        String ret = "";
        Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 获取剪切板中的内容
        Transferable clipTf = sysClip.getContents(null);

        if (clipTf != null) {
            // 检查内容是否是文本类型
            if (clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    ret = (String) clipTf
                            .getTransferData(DataFlavor.stringFlavor);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return ret;
    }

    /**
     * 5.将字符串复制到剪切板。
     */
    public static void setSysClipboardText(String writeMe) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(writeMe);
        clip.setContents(tText, null);
    }

}

